package com.cctc.rds.raft.core.node;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 节点投票器。
 */
public class NodeVoter {
    private long term;
    private int voteNodeId;
    private final AtomicBoolean hasVoted = new AtomicBoolean(Boolean.FALSE);

    public NodeVoter(long term) {
        this.term = term;
    }

    public long getTerm() {
        return term;
    }

    public int getVoteNodeId() {
        return voteNodeId;
    }

    public boolean lock(int nodeId) {
        if (hasVoted.compareAndSet(Boolean.FALSE, Boolean.TRUE)) {
            this.voteNodeId = nodeId;
            return true;
        }
        return false;
    }

}
